/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.document.domain.BookDocuments;
import com.oyun.modules.document.domain.DocPicture;
import com.oyun.modules.document.domain.DocRemPicture;
import com.oyun.modules.document.domain.vo.DocPictureQueryCriteria;
import com.oyun.modules.document.mapper.BookDocumentsMapper;
import com.oyun.modules.document.mapper.DocPictureMapper;
import com.oyun.modules.document.mapper.DocRemPictureMapper;
import com.oyun.modules.document.service.DocPictureService;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author oyun
 * @description 服务实现
 * @date 2024-05-13
 **/
@Service
@RequiredArgsConstructor
public class DocPictureServiceImpl extends ServiceImpl<DocPictureMapper, DocPicture> implements DocPictureService {

    private final DocPictureMapper docPictureMapper;

    private final BookDocumentsMapper bookDocumentsMapper;

    private final DocRemPictureMapper docRemPictureMapper;

    @Override
    public PageResult<DocPicture> queryAll(DocPictureQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(docPictureMapper.findAll(criteria, page));
    }

    @Override
    public List<DocPicture> queryAll(DocPictureQueryCriteria criteria) {
        return docPictureMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DocPicture resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DocPicture resources) {
        DocPicture docPicture = getById(resources.getId());
        docPicture.copy(resources);
        saveOrUpdate(docPicture);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(id != null, DocPicture::getId, id);
            DocPicture docPicture = docPictureMapper.selectOne(wrapper);
            LambdaQueryWrapper<BookDocuments> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(docPicture.getDocId() != null, BookDocuments::getId, docPicture.getDocId());
            BookDocuments bookDocuments = bookDocumentsMapper.selectOne(wrapper1);

            DocRemPicture remPicture = new DocRemPicture();
            remPicture.setDocId(bookDocuments.getId());
            remPicture.setMnName(bookDocuments.getMnName());
            remPicture.setMnNameCode(IntermediateCodeUtils.mn2Code(bookDocuments.getMnName()).replaceAll("-", " -"));
            remPicture.setZhName(bookDocuments.getZhName());
            remPicture.setPath(docPicture.getPath());
            remPicture.setRemTime(new Date());
            docRemPictureMapper.insert(remPicture);
        }
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<DocPicture> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DocPicture docPicture : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" docId", docPicture.getDocId());
            map.put(" filename", docPicture.getFilename());
            map.put(" createBy", docPicture.getCreateBy());
            map.put(" createTime", docPicture.getCreateTime());
            map.put(" updateBy", docPicture.getUpdateBy());
            map.put(" updateTime", docPicture.getUpdateTime());
            map.put(" remark", docPicture.getRemark());
            map.put(" status", docPicture.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DocPicture> getPictureList(Long docId) {
        LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(docId != null, DocPicture::getDocId, docId).eq(DocPicture::getStatus, 1);
        return docPictureMapper.selectList(wrapper);
    }

    @Override
    public Object getFirstPictureById(Long docId) {
        LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocPicture::getFilename, "1.jpg").eq(DocPicture::getDocId, docId).eq(DocPicture::getStatus, 1);
        DocPicture docPicture = docPictureMapper.selectOne(wrapper);
        return docPicture;
    }

    @Override
    public Object getNextPicture(Long docId, Long pageNum) {
        LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.apply("SUBSTRING(filename, 1, 1) = {0}", ++pageNum).eq(DocPicture::getDocId, docId);
        DocPicture docPicture = docPictureMapper.selectOne(wrapper);
        return docPicture;
    }

    @Override
    public Object getPrePicture(Long docId, Long pageNum) {
        LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.apply("SUBSTRING(filename, 1, 1) = {0}", --pageNum).eq(DocPicture::getDocId, docId);
        DocPicture docPicture = docPictureMapper.selectOne(wrapper);
        return docPicture;
    }

    @Override
    public Object skip(Long docId, Long pageNum) {
        LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.apply("SUBSTRING(filename, 1, 1) = {0}", pageNum).eq(DocPicture::getDocId, docId);
        DocPicture docPicture = docPictureMapper.selectOne(wrapper);
        return docPicture;
    }

    @Override
    public Object totality(Long docId) {
        LambdaQueryWrapper<DocPicture> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocPicture::getDocId, docId);
        return docPictureMapper.selectCount(wrapper);
    }
}
